home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / sci / RARS_Amiga_2.lha / RARS / os.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-31  |  5.4 KB  |  273 lines

  1. /*
  2.  * $RCSfile: os.cpp $
  3.  *
  4.  * $Author: marcel $
  5.  *
  6.  * $Revision: 1.4 $
  7.  *
  8.  * $Date: 1995/03/31 15:56:31 $
  9.  *
  10.  * $Locker: marcel $
  11.  *
  12.  * $State: Exp $
  13.  *
  14.  * Amiga version
  15.  *
  16.  * Copyright © 1995 Marcel Offermans
  17.  *
  18.  * tabsize = 5
  19.  */
  20.  
  21. /* includes */
  22. #include <proto/exec.h>
  23. #include <proto/dos.h>
  24. #include <proto/intuition.h>
  25. #include <dos.h>
  26. #include <time.h>
  27. #include <ctype.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include "track.h"
  31. #include "os.h"
  32. #include "car.h"
  33.  
  34. #define TEMPLATE        "TRACK,DRIVERS/M,CARS/N,LAPS/N,RACES/N,KEEPORDER/S,NOREALTIME/S,NODISPLAY/S,NORANDOMSEED/S"
  35. #define OPT_TRACK        0
  36. #define OPT_DRIVERS        1
  37. #define OPT_CARS        2
  38. #define OPT_LAPS        3
  39. #define OPT_RACES        4
  40. #define OPT_KEEPORDER    5
  41. #define OPT_NOREALTIME    6
  42. #define OPT_NODISPLAY    7
  43. #define OPT_NORANDOMSEED    8
  44. #define OPT_COUNT        9
  45.  
  46. /* externals */
  47. extern con_vec(*drovers[])(situation);
  48. extern char *nam_ptr[];
  49. void print_help_file(void);
  50. int find_name(char *);
  51.  
  52. /* globals */
  53. static char            version_ptr[]            = "\0$VER: RARS_Amiga 2.0 " __AMIGADATE__ ;
  54. static int            rndmiz                = 1;
  55. struct RDArgs *        args_ptr                = NULL;
  56. struct IntuiMessage *    imsg_ptr                = NULL;
  57. struct IntuiMessage        imsg;
  58. LONG                    opts[OPT_COUNT];
  59. ULONG                idcmpmask;
  60. ULONG                signals;
  61.  
  62. /* initialize the random number generator based on the current time */
  63. void randomizer(void)
  64. {
  65.     unsigned int clock[2];
  66.     timer(clock);
  67.     srand(clock[1]);
  68.     return;
  69. }
  70.  
  71. /* return a random number between 0 and limit */
  72. double random(int limit)
  73. {
  74.     return(((double)rand() / (double)RAND_MAX) * (double)limit);
  75. }
  76.  
  77. /* convert integer to ascii */
  78. void itoa(int value, char *buffer_ptr, int base)
  79. {
  80.     if (base == 10)
  81.     {
  82.         stci_d(buffer_ptr, value);
  83.     }
  84. }
  85.  
  86. /* get arguments by using a standard command line template */
  87. void get_args(int argc, char* argv[])
  88. {
  89.     int i, n;
  90.     char *temp_ptr;
  91.     colors temp_clr;
  92.     con_vec(*temp_drv)(situation);
  93.     char **driver_ptr_ptr;
  94.  
  95.     /* set up defaults */
  96.     strcpy(trackfile, "trackfil.trk");
  97.     lap_count = 4;
  98.     car_count = 6;
  99.     real_speed = 1;
  100.     race_count = 2;
  101.     keep_order = 0;
  102.  
  103.     /* get arguments */
  104.     if (args_ptr = ReadArgs(TEMPLATE, opts, NULL))
  105.     {
  106.         if (opts[OPT_TRACK])
  107.         {
  108.             /* track file name */
  109.             strcpy(trackfile, (char *)opts[OPT_TRACK]);
  110.         }
  111.         if (opts[OPT_DRIVERS])
  112.         {
  113.             /* list of drivers in the race */
  114.             driver_ptr_ptr = (char **)opts[OPT_DRIVERS];
  115.             for (n = 0; n < MAXCARS; n++)
  116.             {
  117.                 if (driver_ptr_ptr[n] == NULL)
  118.                 {
  119.                     break;
  120.                 }
  121.                 if ((i = find_name(driver_ptr_ptr[n])) < 0)
  122.                 {
  123.                     break;
  124.                 }
  125.                 temp_ptr = nam_ptr[n];
  126.                 nam_ptr[n] = nam_ptr[i];
  127.                 nam_ptr[i] = temp_ptr;
  128.                 temp_clr = car_clrs[n];
  129.                 car_clrs[n] = car_clrs[i];
  130.                 car_clrs[i] = temp_clr;
  131.                 temp_drv = drovers[n];
  132.                 drovers[n] = drovers[i];
  133.                 drovers[i] = temp_drv;
  134.             }
  135.             /* if any drivers were found, set the number of cars to the number of drivers */
  136.             if (n > 0)
  137.             {
  138.                 car_count = n;
  139.             }
  140.         }
  141.         if (opts[OPT_LAPS])
  142.         {
  143.             /* number of laps to go */
  144.             lap_count = (int)(*((LONG *)opts[OPT_LAPS]));
  145.  
  146.             /* check bounds of user input */
  147.             if (lap_count <= 0)
  148.             {
  149.                 lap_count = 1;
  150.             }
  151.         }
  152.         if (opts[OPT_CARS])
  153.         {
  154.             /* number of cars in the race */
  155.             car_count = (int)(*((LONG *)opts[OPT_CARS]));
  156.  
  157.             /* check bounds of user input */
  158.             if (car_count < 0)
  159.             {
  160.                 /* zero cars is legal, and only shows you the track */
  161.                 car_count = 0;
  162.             }
  163.             else if (car_count > MAXCARS)
  164.             {
  165.                 car_count = MAXCARS;
  166.             }
  167.         }
  168.         if (opts[OPT_RACES])
  169.         {
  170.             /* number of races to go */
  171.             race_count = (int)(*((LONG *)opts[OPT_RACES]));
  172.  
  173.             /* check bounds of user input */
  174.             if (race_count <= 0)
  175.             {
  176.                 race_count = 1;
  177.             }
  178.         }
  179.         if (opts[OPT_NOREALTIME])
  180.         {
  181.             /* run as fast as possible instead of real-time */
  182.             real_speed = 0;
  183.         }
  184.         if (opts[OPT_NORANDOMSEED])
  185.         {
  186.             /* don't set a new random seed every time */
  187.             rndmiz = 0;
  188.         }
  189.         if (opts[OPT_NODISPLAY])
  190.         {
  191.             /* don't use a graphics display */
  192.             no_display = 1;
  193.         }
  194.         if (opts[OPT_KEEPORDER])
  195.         {
  196.             /* keep the starting order */
  197.             keep_order = 1;
  198.         }
  199.     }
  200. }
  201.  
  202. /* blocks until one character is read from the keyboard */
  203. int get_ch(void)
  204. {
  205.     if (!available)
  206.     {
  207.         /* if there's no graphics display, we don't handle the keyboard */
  208.         return(0);
  209.     }
  210.  
  211.     /* wait for a message if we don't have a cached message yet */
  212.     while (imsg_ptr == NULL)
  213.     {
  214.         signals = Wait(idcmpmask);
  215.         if (signals & idcmpmask)
  216.         {
  217.             imsg_ptr = (struct IntuiMessage *)GetMsg(window_ptr->UserPort);
  218.             if (imsg_ptr)
  219.             {
  220.                 CopyMem((char *)imsg_ptr, (char *)&imsg, (long)sizeof(struct IntuiMessage));
  221.                 ReplyMsg((struct Message *)imsg_ptr);
  222.             }
  223.         }
  224.     }
  225.     return(imsg.Code);
  226. }
  227.  
  228. /* this routine returns non-zero if there's input available for get_ch() and 0 if not */
  229. int kb_hit(void)
  230. {
  231.     if (!available)
  232.     {
  233.         /* if there's no graphics display, we don't handle the keyboard */
  234.         return(0);
  235.     }
  236.  
  237.     imsg_ptr = (struct IntuiMessage *)GetMsg(window_ptr->UserPort);
  238.     if (imsg_ptr)
  239.     {
  240.         /* we received a message, so now we have to cache it until get_ch() is called */
  241.         CopyMem((char *)imsg_ptr, (char *)&imsg, (long)sizeof(struct IntuiMessage));
  242.         ReplyMsg((struct Message *)imsg_ptr);
  243.         return(1);
  244.     }
  245.     else
  246.     {
  247.         return(0);
  248.     }
  249. }
  250.  
  251. /* busy wait for a tick to go by */
  252. void one_tick(int initialize = 0)
  253. {
  254.     static unsigned int pre_clock[2];
  255.     static unsigned int cur_clock[2];
  256.  
  257.     if (initialize)
  258.     {
  259.         timer(pre_clock);
  260.     }
  261.     else
  262.     {
  263.         do
  264.         {
  265.             timer(cur_clock);
  266.         }
  267.         while (cur_clock[1] + 1000000 * (cur_clock[0] - pre_clock[0]) < pre_clock[1] + (int)(delta_time * 1000000));
  268.  
  269.         pre_clock[0] = cur_clock[0];
  270.         pre_clock[1] = cur_clock[1];
  271.     }
  272. }
  273.